<template>
  <div class="module-title">系统状态</div>
  <div class="system-status-wrap">
    <div class="flex flex-1 flex-col items-center">
      <div class="title-wrap cpu">CPU温度</div>
      <div class="text-2xl">{{ cpuTemperature || '0' }}°C</div>
    </div>
    <div class="flex flex-1 flex-col items-center">
      <div class="title-wrap mem">内存使用率</div>
      <div class="text-2xl">{{ memUsage || '0' }}%</div>
    </div>
  </div>
  <div class="cpu-use">
    <div class="item" v-for="(item, idx) in cpuUsage" :key="idx">
      <div class="core">核{{ idx + 1 }}</div>
      <div class="desc">
        <div>CPU 使用率</div>
        <div>{{ item }}%</div>
      </div>
    </div>
  </div>

  <div class="module-title">磁盘使用率</div>
  <div :id="id" class="h-60 w-full"></div>
</template>

<script setup lang="tsx">
import { onMounted, useId } from 'vue';
import * as echarts from 'echarts';

const id = useId();

interface Props {
  cpuTemperature?: string;
  cpuUsage?: string[];
  memUsage?: string;
  diskUsage?: any[];
}

const props = defineProps<Props>();
const renderChart = () => {
  const chart = echarts.init(document.getElementById(id));
  console.log();
  const xData = [];
  const data = [];
  for (const item of props.diskUsage) {
    xData.push(item.disk);
    data.push(+item.usage.replace('%', ''));
  }
  chart.setOption({
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        data: xData,
        axisTick: {
          alignWithLabel: true,
        },
        axisLine: {
          lineStyle: {
            color: 'rgba(255,255,255,.6)',
          },
        },
      },
    ],
    yAxis: [
      {
        name: '使用率 %',
        type: 'value',
        max: 100,
        splitLine: {
          show: true,
          lineStyle: {
            type: 'dashed',
            color: 'rgba(255,255,255,.26)',
          },
        },
        axisLine: {
          lineStyle: {
            color: 'rgba(255,255,255,.6)',
          },
        },
      },
    ],
    series: [
      {
        type: 'bar',
        barWidth: 40,
        itemStyle: {
          color: '#35c4fa',
        },
        data: data,
      },
    ],
  });
};

onMounted(renderChart);
</script>

<style lang="scss" scoped>
.system-status-wrap {
  width: 588px;
  height: 72px;
  margin: 0 auto 30px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: url('/static/terminal/icon.png') no-repeat center/100% 100%;
}

.title-wrap {
  font-size: 14px;
  color: #fff;
  position: relative;

  &::before {
    content: ' ';
    position: absolute;
    left: -10px;
    top: 10px;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background-color: #fff;
  }

  &.cpu::before {
    background-color: #ffbf80ff;
  }
  &.cpu::mem {
    background-color: #6eb5f5ff;
  }
}

.cpu-use {
  width: 588px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;

  .item {
    display: flex;
    align-items: center;
    margin-bottom: 8px;

    .core {
      width: 48px;
      height: 48px;
      margin-right: 8px;
      text-align: center;
      line-height: 48px;
      color: #fff;
      font-size: 13px;
      font-weight: 600;
      background: url('/static/terminal/core.png') no-repeat center/100% 100%;
    }

    .desc {
      flex: 1;

      div:first-of-type {
        font-size: 14px;
      }

      div:last-of-type {
        font-size: 22px;
      }
    }
  }
}
</style>
